package cn.stylefeng.guns.modular.vlt.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.CommUtils;
import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.vlt.entity.Member;
import cn.stylefeng.guns.modular.vlt.model.params.MemberParam;
import cn.stylefeng.guns.modular.vlt.model.result.MemberResult;
import cn.stylefeng.guns.modular.vlt.service.MemberService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 参加活动人员表控制器
 *
 * @author
 * @Date 2020-12-16 11:07:24
 */
@Controller("vltMemberController")
@RequestMapping("/vlt/member")
public class MemberController extends BaseController {

    private String PREFIX = "/vlt/member";

    @Autowired
    private MemberService memberService;

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2020-12-16
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/member.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2020-12-16
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/member_add.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2020-12-16
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/member_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2020-12-16
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(MemberParam memberParam) {
        this.memberService.add(memberParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2020-12-16
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MemberParam memberParam) {
        this.memberService.update(memberParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2020-12-16
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(MemberParam memberParam) {
        this.memberService.delete(memberParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2020-12-16
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(MemberParam memberParam) {
        Member detail = this.memberService.getById(memberParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2020-12-16
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(MemberParam memberParam) {
        return this.memberService.findPageBySpec(memberParam);
    }

    @RequestMapping("/export")
    public void toHtmlOf07Base(MemberParam memberParam) throws IOException {
        TemplateExportParams params = new TemplateExportParams(
                "template/member.xlsx");
        List<MemberResult> records = this.memberService.findListBySpec(memberParam);
        records.forEach(ob -> {
            ob.setAge(CommUtils.IdNOToAge(ob.getIdCard()));
        });
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (MemberResult groupBasicResult : records) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("age", groupBasicResult.getAge());
            lm.put("memberName", groupBasicResult.getMemberName());
            lm.put("idCard", groupBasicResult.getIdCard());
            lm.put("sex", groupBasicResult.getSex());
            lm.put("address", groupBasicResult.getAddress());
            lm.put("tel", groupBasicResult.getTel());
            lm.put("school", groupBasicResult.getSchool());
            lm.put("hours", groupBasicResult.getHours());
            lm.put("registerDate", DateUtils.formateYYYYMMDD(groupBasicResult.getRegisterDate()));
            listMap.add(lm);
        }
        map.put("maplist", listMap);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        HttpServletResponse response = super.getHttpServletResponse();
        response.reset();

        long dateStr = System.currentTimeMillis();
        // 指定下载的文件名--设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 写出数据输出流到页面
        try {
            OutputStream output = response.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            workbook.write(bufferedOutPut);
            bufferedOutPut.flush();
            bufferedOutPut.close();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //return "success";
    }

}


